package com.xingdian.xingdianhulianserver.interaptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: 喻敏航
 * @Description:拦截器配置
 * @Date: 19:25  2019/3/8
 **/
@Configuration //用于标记当前类是一个配置类（类似Spring中的配置文件），并需要被SpringBoot扫描
public class InterceptorConfig implements WebMvcConfigurer{
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        InterceptorRegistration registration = registry.addInterceptor(new InterfaceInterceptor());



        //定义拦截器不需要拦截的请求路径数组，如果不需要被拦截的请求路径，与需要拦截的路径前缀不同，这个选项可写
        String[] excludePathPatterns = {
                "/",
                "/login",
                "**/*.js",
                "**/*.css"


        };
        registration.excludePathPatterns(excludePathPatterns);

        //定义拦截器可拦截的请求路径数组，** 表示任意目录下的任意请求
        String[] addPathPatterns = {
                "/upload",
                "/toupload",
                "/add",
                "/update",
                "/updateInfo",
                "/toadd",
                "/tomain",
                "/findinfo",
                "/delInfo"

        };
        registration.addPathPatterns(addPathPatterns);
    }


    //所有的WebMvcConfigureAdapter     设置默认启动页面，HelloController下的方法和这个二选一
    @Bean
    //将组件注册到容器中
    public WebMvcConfigurer webMvcConfigurer() {
        WebMvcConfigurer configurer = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                //它会找templates文件夹下的文件是因为模版映射的关系,templates为Spring Boot默认jar包使用嵌入式的Tomcat
                registry.addViewController("/").setViewName("index");
            }

            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {}
        };
        return configurer;
    }
}
